package jmine.tec.aom.dao;

import java.util.List;

import jmine.tec.aom.domain.Property;
import jmine.tec.aom.domain.PropertyGroup;
import jmine.tec.aom.si.PropertySystemIdentifier;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.persist.dao.BeanNotFoundException;
import bancosys.tec.persist.dao.SystemEntityDAO;

/**
 * DAO for {@link Property}
 * 
 * @author jrenaut
 */
public class PropertyDAO extends SystemEntityDAO<Property, PropertySystemIdentifier> {

    /**
     * @param group a {@link PropertyGroup}
     * @param name the name of the {@link Property}
     * @return a {@link Property}
     * @throws BeanNotFoundException if none was found
     */
    public Property findByGroupAndName(PropertyGroup group, String name) throws BeanNotFoundException {
        return this.executeSingleQuery(this.createCriteria().add(Restrictions.eq("propertyGroup", group))
                .add(Restrictions.eq("name", name)));
    }

    /**
     * @param group a {@link PropertyGroup}
     * @param mnemonic the mnemonic of the {@link Property}
     * @return a {@link Property}
     * @throws BeanNotFoundException if none was found
     */
    public Property findByGroupAndMnemonic(PropertyGroup group, String mnemonic) throws BeanNotFoundException {
        return this.executeSingleQuery(this.createCriteria().add(Restrictions.eq("propertyGroup", group))
                .add(Restrictions.eq("mnemonic", mnemonic)));
    }

    /**
     * @param groupMnemonic the {@link PropertyGroup} mnemonic
     * @param mnemonic the {@link Property} mnemonic
     * @return a {@link Property}
     * @throws BeanNotFoundException if none was found
     */
    public Property findByGroupAndMnemonic(String groupMnemonic, String mnemonic) throws BeanNotFoundException {
        Criteria crit = this.createCriteria();
        crit.add(Restrictions.eq("mnemonic", mnemonic));
        crit.createCriteria("propertyGroup").add(Restrictions.eq("mnemonic", groupMnemonic));
        return this.executeSingleQuery(crit);
    }

    /**
     * @param group a {@link PropertyGroup}
     * @return a {@link List} of {@link Property}
     */
    public List<Property> findByGroup(PropertyGroup group) {
        return this.executeQuery(this.createCriteria().add(Restrictions.eq("propertyGroup", group)));
    }

    /**
     * @param group a {@link PropertyGroup}
     * @return a {@link List} of {@link Property}
     */
    public List<Property> findByGroupOrderedByPk(PropertyGroup group) {
        return this.executeQuery(this.createCriteria().add(Restrictions.eq("propertyGroup", group)).addOrder(Order.asc("id")));
    }
}
